//
//  QZMyAppointVc.m
//  WeiWeiHaircut
//
//  Created by 上官惠阳 on 15/12/11.
//  Copyright © 2015年 超群. All rights reserved.
//

#import "QZMyAppointVc.h"
#import "QZMyOrderCell.h"
#import "QZMyAppointDeatilVc.h"
#import "QZComplaintVc.h"
#import "AppointmentModel.h"
#import "MJRefresh.h"
@interface QZMyAppointVc ()

@end

@implementation QZMyAppointVc
{
    NSMutableArray *_topBtnArr;
    NSInteger _order_type;//选中的是哪一个
    NSInteger _pageno;//页数
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _topBtnArr = [[NSMutableArray alloc]init];
    _pageno = 1;
    _order_type = -1;
    [self titleWithSting:@"我的预约"];
    
    [self addBtnOnNavWithTitle:@"我" andImageName:@"返回" andTarget:self andAction:@selector(backAction) andFrame:CGRectMake(0, 0, 35, 20) andDirection:left];
    
//    [self addBtnOnNavWithTitle:@"投诉" andImageName:nil andTarget:self andAction:@selector(complaintAction) andFrame:CGRectMake(0, 0, 35, 20) andDirection:right];
    
    [self createTableViewWithPlainStyle:YES andSeparatorStyleNone:YES andBackGroudImageName:nil];
    self.tableView.backgroundColor = BACKGROUDCOLOR;
    
    [self registCellWithNib:@"QZMyOrderCell" addIdentifier:@"myOrderCell" addHeight:195];
    
    [self createHeadView];

    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageno = 1;
        [self loadData];
        
    }];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _pageno ++;
        [self loadData];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    

    [self loadData];
    
    
}
//投诉
-(void)complaintAction
{
    QZComplaintVc *camplainVc = [[QZComplaintVc alloc]init];
    [self.navigationController pushViewController:camplainVc animated:YES];
}
-(void)loadData
{


    NSString *url = [NSString stringWithFormat:@"%@%@",newPublicURL,@"HairOrder.GetHairOrderUser"];
    NSDictionary *dic = @{
                          @"user_id":[QZUserInfo userId],
                          @"order_type":@(_order_type),
                          @"page":@(_pageno),
                          @"limit":@(8),
                          @"sort":@"time",
                          @"sort_type":@"right",
                          @"user_type":@(1)
                          };
    if (self.tableSource.count>0) {
        self.request.isShowHUD = NO;
    }else{
        self.request.isShowHUD = YES;
    }
    [self.request QZRequest_POST:url parameters:dic tagNSString:@"预约列表" stopRequest:NO isSerializer:NO isCache:YES];
}
-(void)createHeadView
{
    NSArray* array = @[@"全部",@"未付押",@"待付款",@"待服务",@"已完成",@"纠纷",@"未受理"];
    CGFloat bw = WIDTH/array.count;
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    topView.backgroundColor = BACKGROUDCOLOR;
    
    for (int i = 0 ; i<array.count; i++) {
        
        UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(i*(bw+1), 0,bw , 30)];
        button.backgroundColor = [UIColor whiteColor];
        [button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:SBpurpleColor forState:UIControlStateSelected];
        [button setTitleColor:TabBarTitleColor forState:UIControlStateNormal];
        button.tag = 100+i;
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        if (i == 0) {
            button.selected = YES;
        }
        [topView addSubview:button];
        [_topBtnArr addObject:button];
    }
    for (int i = 1; i<array.count; i++) {
        UIView* linView = [[UIView alloc]initWithFrame:CGRectMake(i*bw+i, 0, 1, 30)];
        linView.backgroundColor = BACKGROUDCOLOR;
        [topView addSubview:linView];
    }
    [self.view addSubview:topView];
    
    self.tableView.frame = CGRectMake(0, 30, WIDTH, HEIGHT - 30 - 64);
}
- (void) buttonTap:(UIButton*)sender
{
    for (UIButton *btn in _topBtnArr) {
        btn.selected = NO;
    }
    sender.selected = YES;
    
    switch (sender.tag-100) {
        case 0:
            _order_type = -1;
            //@[@"全部",@"未付押金",@"待付款",@"待服务",@"待评价",@"纠纷"];
            break;
        case 1:
            _order_type = 0;
            break;
        case 2:
            _order_type = 5;
            break;
        case 3:
            _order_type = 1;
            break;
        case 4:
            _order_type = 2;
            break;
        case 5:
            _order_type = 3;
            break;
        case 6:
            _order_type = 4;
            break;
        default:
            break;
    }
    _pageno = 1;
    [self loadData];
    
}
-(void)loadCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    QZMyOrderCell *myOrderCell = (QZMyOrderCell *)cell;
    myOrderCell.model = self.tableSource[indexPath.row];
}
-(void)actionAtIndexPath:(NSIndexPath *)indexPath
{
    QZMyAppointDeatilVc *detailVc = [[QZMyAppointDeatilVc alloc]init];
    detailVc.model = self.tableSource[indexPath.row];
    [self.navigationController pushViewController:detailVc animated:YES];
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)request_POST_FinishValue:(id)value tagNSString:(NSString *)tag{
    if ([tag isEqualToString:@"预约列表"]) {
        if (_pageno == 1) {
            [self.tableSource removeAllObjects];
        }
        if ([value[@"data"][@"status"] integerValue] == 100) {
            NSArray *array= value[@"data"][@"data"][@"list"];
            for (NSDictionary *dic in array) {
                AppointmentModel *model = [[AppointmentModel alloc] initWithDictionary:dic error:nil];
                [self.tableSource addObject:model];
            }
            
        }else{
            NSString  *str= [NSString stringWithFormat:@"%@",value[@"data"][@"info"]];
            [[iToast makeText:@"无数据"] show];
        }
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    }
    NSLog(@"%@",value);
}

@end
